#include "..\inc\SendRecvManager.h"

//#include <UDTClient.h>
#include <UdtClientSession.h>
#include <Poco/File.h>
#include <QString>
#include <Poco/NamedEvent.h>
SendRecvManager::SendRecvManager(void)
{
    iRetryNumber = 24;
	ID= "";
}

SendRecvManager::SendRecvManager(int aType, const std::string& aPathFile, Poco::Int64 aSession , const std::string& aDir)
{
	iType = aType;
	iPathFile = aPathFile;
	iSession = aSession;
	iDir = aDir;
        iRetryNumber = 24;
	ID = "";
}

SendRecvManager::~SendRecvManager(void)
{
}

//void SendRecvManager::Execute()
//{
//	UDT::startup();
//        UDTClient aClient(iSession , Poco::Net::SocketAddress( "3go.vn", 9000) );
//
//        if (! aClient.ConnectWithPeerImp() )
//        {
//            emit ConnectFailed();
//            return;
//        }
//        emit ConnectedToPeer();
//
//	iClient = aClient.GetPeerSocket();
//
//        CUdtClientSession aClientSession(iClient ,iPathFile );
//        CUdtClientSession * aSession = & aClientSession;
//        connect(aSession, SIGNAL(SentFileDescriptions(bool)) , this , SIGNAL(SentFileDescription(bool) ) );
//        connect(aSession, SIGNAL(ReceivedFileDescriptions(bool)) , this , SIGNAL(ReceivedFileDescription(bool)) );
//        connect(aSession , SIGNAL(PieceReceived(int)) , this , SIGNAL(PieceReceived(int)) );
//        connect(aSession, SIGNAL(PieceSent(int)) , this , SIGNAL(PieceSent(int)) );
//        connect(aSession, SIGNAL(TotalFileSizeInfo(Poco::UInt64)) , this , SIGNAL(TotalFileSizeInfo(Poco::UInt64)) );
//        connect(aSession , SIGNAL(TotalPiecesCount(int)) , this , SIGNAL(TotalPiecesCount(int)) );
//
//
//	if(eSend  == iType )
//	{
//		aClientSession.setNameSession(iDir);
//		int aLoop = 0;
//		while (aLoop< 10)
//		{
//			if (aClientSession.SenderWaitingRequest())
//			{
//				return;
//			}
//			else
//				aLoop++;
//		}
//		Poco::File aDeleteTmp(iDir);
//		if(aDeleteTmp.exists())
//			aDeleteTmp.remove(true);
//		//aClientSession.SenderWaitingRequest();
//	}
//	else if(eRecv == iType )
//	{
//		aClientSession.setNameSession(iDir);
//		int aLoop = 0;
//		while (aLoop < 10)
//		{
//			if ( !aClientSession.getFileDescription())
//			{
//				aLoop++;
//				continue;
//			}
//			std::list<int> aListUnsuccessful = aClientSession.receiveAllPiece();
//			if (aListUnsuccessful.size() >0 )
//			{
//				aLoop++;
//			}
//			else if(aListUnsuccessful.size() == 0)
//				return;
//		}
//
//		Poco::File aDeleteTmp(iDir);
//		if(aDeleteTmp.exists())
//			aDeleteTmp.remove(true);
//		/*if(aClientSession.getFileDescription())
//			aClientSession.receiveAllPiece();*/
//	}
//
//        //emit Finished();
//
//}
#include <Poco/FileStream.h>
void SendRecvManager::Execute()
{
    UDT::startup();

    CUdtClientSession aClientSession(iClient ,iPathFile );

    CUdtClientSession * aSession = & aClientSession;
    connect(aSession, SIGNAL(SentFileDescriptions(bool)) , this , SIGNAL(SentFileDescription(bool) ) );
    connect(aSession, SIGNAL(ReceivedFileDescriptions(bool)) , this , SIGNAL(ReceivedFileDescription(bool)) );
    connect(aSession , SIGNAL(PieceReceived(int)) , this , SIGNAL(PieceReceived(int)) );
    connect(aSession, SIGNAL(PieceSent(int)) , this , SIGNAL(PieceSent(int)) );
    connect(aSession, SIGNAL(TotalFileSizeInfo(Poco::UInt64)) , this , SIGNAL(TotalFileSizeInfo(Poco::UInt64)) );
    connect(aSession , SIGNAL(TotalPiecesCount(int)) , this , SIGNAL(TotalPiecesCount(int)) );

	connect(aSession , SIGNAL(finished()) , this , SIGNAL(finished()) );


    int aLoop = 0;
    while (aLoop < iRetryNumber)
    {
		//cout<<" loop " <<aLoop <<endl;
        UDTClient aClient(iSession , Poco::Net::SocketAddress( "3go.vn", 9000) );

        if (! aClient.ConnectWithPeerImp() )
        {
            emit ConnectFailed();
            aLoop++;
            continue;
        }
        emit ConnectedToPeer();

        if (aClient.IsP2P())
        {
            Poco::FileOutputStream aFile(iDir + "/p2p.mark");
            aFile<<"true";
        }
        iClient = aClient.GetPeerSocket();

        aClientSession.SetSocket(iClient);


        if(eSend  == iType )
        {
            aClientSession.setNameSession(iDir);

            if (aClientSession.SenderWaitingRequest())
            {
                return;
            }
            else
            {
                aLoop++;
                continue;
            }

            Poco::File aDeleteTmp(iDir);
            if(aDeleteTmp.exists())
                aDeleteTmp.remove(true);
            //aClientSession.SenderWaitingRequest();
        }
        else if(eRecv == iType )
        {
            aClientSession.setNameSession(iDir);

            if ( !aClientSession.getFileDescription())
            {
                aLoop++;
                continue;
            }
            std::list<int> aListUnsuccessful = aClientSession.receiveAllPiece();
            if (aListUnsuccessful.size() >0 )
            {
                aLoop++;
				continue;
            }
            else if(aListUnsuccessful.size() == 0)
			{
				if(ID != "")
				{
					Poco::NamedEvent aEvent(ID);
					aEvent.set();
				}
				Poco::File aDeleteTmp(iDir);
				if(aDeleteTmp.exists())
					aDeleteTmp.remove(true);

                                QThread::sleep(5);
                return;

			}

            Poco::File aDeleteTmp(iDir);
            if(aDeleteTmp.exists())
                aDeleteTmp.remove(true);
        }

    }

        //emit Finished();

}
